const { resolve } = require('./utils');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: {
        main: resolve('src/main.tsx')
    },
    output: {
        path: resolve('dist')
    },
    devtool: 'none', // inline-source-map 可生成map
    resolve: {
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
        alias: {
            '@': resolve('src')
        }
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx|ts|tsx)$/,
                use: [
                    'babel-loader',
                    'eslint-loader'
                ],
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.less$/,
                use: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            modules: {
                                auto: (resourcePath) => {
                                    // 全局公共 css 不做模块化处理
                                    return !resourcePath.includes('styles')
                                },
                                localIdentName: '[path]-[local]-[hash:base64:5]',
                            }
                        }
                    },
                    'less-loader']
            },
            {
                test: /\.(ttf|woff|woff2|eot|svg|png|jpg|jpeg|gif|webp)$/,
                use: 'url-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({                     // HTML的title
            title: 'react-zero',
            minify: {                               // 压缩HTML文件
                removeComments: true,               // 移除HTML中的注释
                collapseWhitespace: true,           // 删除空白符与换行符
                minifyCSS: true                     // 压缩内联css
            },
            favicon: 'public/favicon.ico',          // ico图标
            filename: 'index.html',                 // 输出文件名
            template: resolve('public/index.html'), // 页面模板地址，默认使用空的html模板
            chunks: ['main']                        // 页面需要注入的js，默认将entry中所有的chunk注入到模板中
        })
    ]
};
